<?php

/*
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

include_once (dirname(__FILE__)."/gallery.config.php");

class Slots {

    function set ($input) {
        $slot = array_keys (get_class_vars (get_class ($this)));
        if (is_array ($input)) {
            foreach ($input as $key => $value) {
                if (in_array ($key, $slot)) {
                    $this->$key = $value;
                }
            }
        }
    }

}

class Element {

    var $type;
    var $_class;
    var $content;
    
    function __construct ($type) {
        $this->type = $type;
    }

    function generate () {
        return '<' . $this->type . (!empty ($this->_class) ? ' class="' . $this->_class . '"':'') . '>' . $this->content . '</' . $this->type . '>';
    }

}

class Anker {

    var $href;
    var $title;
    var $target;
    var $link;
    
    function generate () {
        return '<a href="' . $this->href . '" title="' . $this->title . '"' . (!empty ($this->target) ? ' target="' . $this->target . '"':'') . '>' . $this->link . '</a>';
    }

}

class Image {

    var $src;
    var $alt;
    
    function generate () {
        return '<img src="' . $this->src . '" alt="' . $this->alt . '" />';
    }

}

class flickrPhoto extends Slots {
    
    var $id;
    var $farm;
    var $server;
    var $secret;
    var $title;

    function url ($size = 's') {
        return
            'http://farm' .
            $this->farm .
            '.static.flickr.com/' .
            $this->server .
            '/' .
            $this->id .
            '_' .
            $this->secret .
            (in_array ($size, array ('m', 's', 't', 'b')) ? '_' . $size : '') .
            '.jpg';
    }
    
}

class flickrGallery extends Slots {

    var $params;
    var $stat;
    var $code;
    var $message;

    function __construct() {
        $this->params =
            array (
            	'api_key'	=> FLICKR_API_KEY,
            	'user_id'	=> FLICKR_USER_ID,
            	'format'	=> 'php_serial',
            );
    }

    function request () {
        $encoded_params = array();
        foreach ($this->params as $k => $v) {
        	$encoded_params[] = urlencode ($k) . '=' . urlencode ($v);
        }
        $url = "http://api.flickr.com/services/rest/?" . implode ('&', $encoded_params);
        $rsp = unserialize (file_get_contents ($url));
        $this->set ($rsp);
        if (!$this->error ()) {
            return $rsp;
        }
        return array ();
    }
    
    function error () {
        if ($this->stat != 'ok') {
            return
                array (
                    'stat' => $this->stat,
                    'code' => $this->code,
                    'message' => $this->message
                );
        }
        return array ();
    }

}

class flickrPhotoSearch extends flickrGallery {

    var $total;
    var $pages;
    var $perpage;
    var $page;
    var $id;

    var $fpObjects = array ();

    function __construct ($request) {
        parent::__construct ();
        $this->params['method']	= 'flickr.photos.search';
        $this->params['per_page'] = MAX_PER_PAGE;
        if ($request->pg > 1) {
            $this->params['page'] = $request->pg;
        }
        $this->id = $request->id;
    }

    function get () {
        $rsp = $this->request ();
        if (isset ($rsp['photos'])) {
            $this->set ($rsp['photos']);
            for ($i = 0; $i < count ($rsp['photos']['photo']); $i++) {
                $this->fpObjects[$i] = new flickrPhoto ();
                $this->fpObjects[$i]->set ($rsp['photos']['photo'][$i]);
            }
        }
    }

}

class galleryRequest {

    var $pg = 1;
    var $id = 0;

    function __construct () {
        if (isset ($_REQUEST['pg']) && is_numeric ($_REQUEST['pg'])) {
            $this->pg = $_REQUEST['pg'];
        }
        if (isset ($_REQUEST['id']) && is_numeric ($_REQUEST['id'])) {
            $this->id = $_REQUEST['id'];
        }
    }

    function page ($i) {
        if ($this->pg != $i) {
            $ret = new Anker ();
            $ret->href = $_SERVER['PHP_SELF'] . ($i > 1 ? '?pg=' . $i : '');
            $ret->title = STR_PAGE . ' ' . $i;
            $ret->link = $i;
        }
        else {
            $ret = new Element ('strong');
            $ret->content = $i;
        }
        return $ret->generate (); 
    }

    function id ($n, $obj) {
        $args = array ();
        if ($this->pg > 1) {
            $args[] = 'pg=' . $this->pg; 
        }
        if ($n > 0) {
            $args[] = 'id=' . $n;
        }
        $img = new Image ();
        $img->src = $obj->url ();
        $img->alt = $obj->title;
        $a = new Anker ();
        $a->href = $_SERVER['PHP_SELF'] . (!empty ($args) ? '?' . implode ('&amp;', $args) : '');
        $a->title = $obj->title;
        $a->link = $img->generate ();
        $div = new Element ('div');
        $div->_class = ($n < MAX_PER_PAGE - 1 ? 'e':'r') . 'Thumb';
        $div->content = $a->generate ();
        return $div->generate ();
    }

    function big ($fps) {
        $img = new Image ();
        $img->src = $fps->fpObjects[$this->id]->url ('-');
        $img->alt = $fps->fpObjects[$this->id]->title;
        $a = new Anker ();
        $a->href = $fps->fpObjects[$this->id]->url ('b');
        $a->title = $fps->fpObjects[$this->id]->title;
        $a->target = '_blank';
        $a->link = $img->generate ();
        return $a->generate ();
    }

    function prev () {
        if ($this->id + $this->pg - 1 > 0) {
            $args = array ();
            if ($this->id != 0) {
                if ($this->pg > 1) {
                    $args[] = 'pg=' . $this->pg; 
                }
                if ($this->id - 1 > 0) {
                    $args[] = 'id=' . ($this->id - 1);
                }
            }
            else {
                if ($this->pg - 1 > 1) {
                    $args[] = 'pg=' . ($this->pg - 1);
                }
                $args[] = 'id=' . (MAX_PER_PAGE - 1);
            }
            $ret = new PrevImage ();
            $ret->href = $_SERVER['PHP_SELF'] . (!empty ($args) ? '?' . implode ('&amp;', $args) : '');
        }
        else {
            $ret = new PrevImg0 ();
        }
        return $ret->generate ();
    }

    function next ($n) {
        if ($this->id + 1 + (($this->pg - 1) * MAX_PER_PAGE) < $n) {
            $args = array ();
            if ($this->id + 1 != MAX_PER_PAGE) {
                if ($this->pg > 1) {
                    $args[] = 'pg=' . $this->pg; 
                }
                $args[] = 'id=' . ($this->id + 1);
            }
            else {
                $args[] = 'pg=' . ($this->pg + 1); 
            }
            $ret = new NextImage ();
            $ret->href = $_SERVER['PHP_SELF'] . (!empty ($args) ? '?' . implode ('&amp;', $args) : '');
        }
        else {
            $ret = new NextImg0 ();
        }
        return $ret->generate ();
    }

    function pprev () {
        if ($this->pg > 1) {
            $ret = new PrevPage ();
            $ret->href = $_SERVER['PHP_SELF'] . ($this->pg - 1 != 1 ? '?pg=' . ($this->pg - 1):'');
        }
        else {
            $ret = new PrevPage0 ();
        }
        return $ret->generate ();
    }

    function pnext ($n) {
        if ($this->pg < $n) {
            $ret = new NextPage ();
            $ret->href = $_SERVER['PHP_SELF'] . '?pg=' . ($this->pg + 1);
        }
        else {
            $ret = new NextPage0 ();
        }
        return $ret->generate ();
    }

}

class PrevImage extends Anker {

    function __construct () {
        $link = new PrevImg ();
        $this->title = PREV_IMG_ALT;
        $this->link = $link->generate ();
    }

}

class PrevImg extends Image {

    function __construct () {
        $this->src = PREV_IMG_SRC;
        $this->alt = PREV_IMG_ALT;
    }

}

class PrevImg0 extends Image {
    
    function __construct () {
        $this->src = PREV_IMG0_SRC;
        $this->alt = PREV_IMG0_ALT;
    }

}

class NextImage extends Anker {

    function __construct () {
        $link = new NextImg ();
        $this->title = NEXT_IMG_ALT;
        $this->link = $link->generate ();
    }

}

class NextImg extends Image {

    function __construct () {
        $this->src = NEXT_IMG_SRC;
        $this->alt = NEXT_IMG_ALT;
    }

}

class NextImg0 extends Image {
    
    function __construct () {
        $this->src = NEXT_IMG0_SRC;
        $this->alt = NEXT_IMG0_ALT;
    }

}

class PrevPage extends Anker {

    function __construct () {
        $link = new PrevPageImg ();
        $this->title = PREV_PAGE_ALT;
        $this->link = $link->generate ();
    }

}

class PrevPageImg extends Image {

    function __construct () {
        $this->src = PREV_PAGE_SRC;
        $this->alt = PREV_PAGE_ALT;
    }

}

class PrevPage0 extends Image {
    
    function __construct () {
        $this->src = PREV_PAGE0_SRC;
        $this->alt = PREV_PAGE0_ALT;
    }

}

class NextPage extends Anker {

    function __construct () {
        $link = new NextPageImg ();
        $this->title = NEXT_PAGE_ALT;
        $this->link = $link->generate ();
    }

}

class NextPageImg extends Image {

    function __construct () {
        $this->src = NEXT_PAGE_SRC;
        $this->alt = NEXT_PAGE_ALT;
    }

}

class NextPage0 extends Image {
    
    function __construct () {
        $this->src = NEXT_PAGE0_SRC;
        $this->alt = NEXT_PAGE0_ALT;
    }

}

?>
